﻿using RpcHelper;
using RpcModel;
using RpcSync.Collect;
using RpcSync.Collect.Model;
using RpcSync.Model;
using RpcSync.Service.Interface;
using RpcSync.Service.Node;

namespace RpcSync.Service.Broadcast
{
    [RpcClient.Attr.UnityName("MerOnly")]
    internal class LoadMerOnlyNode : IInitBroadcast
    {
        private IRemoteServerGroupCollect _Server;
        private INodeService _NodeService;
        public LoadMerOnlyNode(IRemoteServerGroupCollect server, INodeService nodeService)
        {
            _NodeService = nodeService;
            _Server = server;
        }

        public void InitBroadcastBody(BroadcastMsg msg, MsgSource source, ref BroadcastBody body)
        {
            List<RootNode> nodes = new List<RootNode>();
            msg.TypeVal.ForEach(a => _NodeService.Load(a, nodes));
            if (nodes.Count == 0)
            {
                throw new ErrorException("rpc.sync.node.null");
            }
            long rpcMerId = msg.RpcMerId == 0 ? source.RpcMerId : msg.RpcMerId;
            MerServer[] servers = this._Server.GetAllServer(rpcMerId);
            body.RpcMerId = rpcMerId;
            body.Dictate = servers.GetDictate(nodes, msg.RegionId);
        }
    }
}
